package com.dhms.vostok1.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.dhms.devicecenter.R
import com.dhms.devicecenter.databinding.DeviceCenterListItemBinding

import com.dhms.vostok1.data.TaskAndDeviceItem
import com.dhms.vostok1.model.DeviceViewModel


class DeviceListAdapter(
    private val onItemClick: (item: TaskAndDeviceItem, index: Int) -> Unit = { _, _ -> }
) :
    ListAdapter<TaskAndDeviceItem, DeviceListAdapter.DeviceListViewHolder>(
        DiffCallback
    ) {

    class DeviceListViewHolder(var _binding: DeviceCenterListItemBinding) :
        RecyclerView.ViewHolder(_binding.root) {

        fun bind(item: TaskAndDeviceItem) {
            _binding.item = item
            var stateResId = R.drawable.device_state_offline
            item.itemStatus?.let {
                if (it == DeviceViewModel.DEVICE_STATE_RUNNING_TEXT) {
                    stateResId = R.drawable.device_state_run
                } else if (it == DeviceViewModel.DEVICE_STATE_STOP_TEXT) {
                    stateResId = R.drawable.device_state_stop
                }
            }
            _binding.deviceStateView.setBackgroundResource(stateResId)
            val itemPerilSum = item.itemPerilSum ?: 0
            val itemFaultSum = item.itemFaultSum ?: 0

            var perilImgVisibility = View.GONE
            var faultImgVisibility = View.GONE
            val perilImgResId = R.drawable.ic_peril_device
            var faultImgResId = R.drawable.ic_fault_device

            if (itemPerilSum > 0 && itemFaultSum > 0) {
                perilImgVisibility = View.VISIBLE
                faultImgVisibility = View.VISIBLE
            } else if (itemPerilSum > 0 || itemFaultSum > 0) {
                faultImgVisibility = View.VISIBLE
            }
            if (itemPerilSum > 0 && itemFaultSum <= 0) {
                faultImgResId = R.drawable.ic_peril_device
            }

            _binding.perilImgView.apply {
                setImageDrawable(
                    ContextCompat.getDrawable(
                        context,
                        perilImgResId
                    )
                )
            }
            _binding.faultImgView.apply {
                setImageDrawable(
                    ContextCompat.getDrawable(
                        context,
                        faultImgResId
                    )
                )
            }

            _binding.perilImgView.visibility = perilImgVisibility
            _binding.faultImgView.visibility = faultImgVisibility
            _binding.executePendingBindings()
        }
    }

    companion object DiffCallback : DiffUtil.ItemCallback<TaskAndDeviceItem>() {

        override fun areItemsTheSame(
            oldItem: TaskAndDeviceItem,
            newItem: TaskAndDeviceItem
        ): Boolean {
            return oldItem.itemId == newItem.itemId
        }

        override fun areContentsTheSame(
            oldItem: TaskAndDeviceItem,
            newItem: TaskAndDeviceItem
        ): Boolean {
            return oldItem == newItem
        }
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): DeviceListViewHolder {
        return DeviceListViewHolder(
            DeviceCenterListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
    }

    override fun onBindViewHolder(holder: DeviceListViewHolder, position: Int) {
        val record = getItem(position)
        holder.bind(record)

        holder._binding.itemCardView.setOnClickListener {
            onItemClick(record, position)
        }

    }
}